package com.lsh.oms.core.service.payment;

import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.enums.*;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.model.po.OmsTask;
import com.lsh.oms.core.service.order.OrderQueryService;
import com.lsh.oms.core.service.po.OmsTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Project Name: OrderSignForRefundService
 * 北京链商电子商务有限公司
 *
 * @author: wangliutao
 * Date: 18/6/23
 * Package Name: com.lsh.oms.core.service.payment
 * Description:
 */
@Service
public class OrderSignForRefundService {
    private static final Logger logger = LoggerFactory.getLogger(OrderSignForRefundService.class);
    private static final String CANCEL_TYPE = "type";

    @Autowired
    private OrderQueryService orderQueryService;

    @Autowired
    private OmsTaskService omsTaskService;


    /**
     * @param orderCode
     */
    public void orderFinishForCoupon(Long orderCode) {
        if (orderCode == null) {
            throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "orderCode is null");
        }

        OrderHead orderHead = orderQueryService.findByCode(orderCode, false, false);
        if (orderHead == null) {
            throw new BusinessException(ReturnCode.REQUEST_FAIL, "查询不到订单!!");
        }

        if (!orderHead.getOrderStatus().equals(OrderStatus.ORDER_SIGN.getIndex()) && !orderHead.getOrderStatus().equals(OrderStatus.ORDER_FINISH.getIndex())) {
            logger.info("【order finish--check】 订单状态, 不是已送达和已完成!! orderCode: {}", orderCode);
            return;
        }

        String logSign = "【order finish -- check】";
        // 组合参数, 插入omsTask表中
        OmsTask filter = new OmsTask();
        filter.setRefCode(orderCode.toString());
        filter.setTaskType(TaskType.ORDER_COUPON.getCode());
        OmsTask task = omsTaskService.getTaskOneByFilter(filter);

        // 判断omsTask是否存在记录, 存在则update, 不存在则插入
        OmsTask omsTask = new OmsTask();
        if (task != null) {
            return;
        }

        logger.info("{} insert omsTask! orderCode: {}", logSign, orderCode);
        omsTask.setRefCode(orderCode.toString());
        omsTask.setTaskType(TaskType.ORDER_COUPON.getCode());
        omsTask.setTaskStatus(TaskStatus.TASK_UN_DEAL.getCode());
        omsTask.setContent(orderCode.toString());
        omsTask.setVenderId(orderHead.getVenderId());
        omsTaskService.add(omsTask);
    }

    /**
     * @param orderCode
     */
    public void orderSignForRefund(Long orderCode) {
        if (orderCode == null) {
            throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "orderCode is null");
        }

        OrderHead orderHead = orderQueryService.findByCode(orderCode, false, false);
        if (orderHead == null) {
            throw new BusinessException(ReturnCode.REQUEST_FAIL, "查询不到订单!!");
        }
        // TODO 退款 通知商城 // 校验orderHead的状态
        String logSign = "【签收--check】";
        if (this.validationOrderStatus(orderHead)) {
            // 组合参数, 插入omsTask表中
            OmsTask filter = new OmsTask();
            filter.setRefCode(orderCode.toString());
            filter.setTaskType(TaskType.REFUND.getCode());
            OmsTask task = omsTaskService.getTaskOneByFilter(filter);

            // 判断omsTask是否存在记录, 存在则update, 不存在则插入
            OmsTask omsTask = new OmsTask();
            if (task != null) {
                logger.info("{} update omsTask! orderCode: {}", logSign, orderCode);
                omsTask.setId(task.getId());
                omsTask.setExecCount(0);
                omsTask.setTaskStatus(TaskStatus.TASK_UN_DEAL.getCode());
                omsTaskService.update(omsTask);
            } else {
                logger.info("{} insert omsTask! orderCode: {}", logSign, orderCode);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put(CANCEL_TYPE, OrderRefundTypeEnum.ORDER_CANCEL_SYSTEM.getValue());
                omsTask.setExt(jsonObject.toJSONString());
                omsTask.setRefCode(orderCode.toString());
                omsTask.setTaskType(TaskType.REFUND.getCode());
                omsTask.setVenderId(orderHead.getVenderId());
                omsTaskService.add(omsTask);
            }
        }
    }

    /**
     * @param orderCode
     */
    public void orderCancelForRefund(Long orderCode) {
        if (orderCode == null) {
            throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "orderCode is null");
        }

        OrderHead orderHead = orderQueryService.findByCode(orderCode, false, false);
        if (orderHead == null) {
            throw new BusinessException(ReturnCode.REQUEST_FAIL, "查询不到订单!!");
        }

//        if (orderHead.getOrderStatus().equals(OrderStatus.ORDER_SIGN.getIndex())) {
//            logger.info("【签收--check】 订单状态, 不是已送达和已完成!! orderCode: {}", orderCode);
//            return ;
//        }

        // TODO 退款 通知商城 // 校验orderHead的状态
        String logSign = "【取消--check】";

        // 组合参数, 插入omsTask表中
        OmsTask filter = new OmsTask();
        filter.setRefCode(orderCode.toString());
        filter.setTaskType(TaskType.REFUND.getCode());
        OmsTask task = omsTaskService.getTaskOneByFilter(filter);

        // 判断omsTask是否存在记录, 存在则update, 不存在则插入
        OmsTask omsTask = new OmsTask();
        if (task != null) {
            logger.info("{} update omsTask! orderCode: {}", logSign, orderCode);
            omsTask.setId(task.getId());
            omsTask.setExecCount(0);
            omsTask.setTaskStatus(TaskStatus.TASK_UN_DEAL.getCode());
            omsTaskService.update(omsTask);
        } else {
            logger.info("{} insert omsTask! orderCode: {}", logSign, orderCode);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(CANCEL_TYPE, OrderRefundTypeEnum.ORDER_CANCEL_SYSTEM.getValue());
            omsTask.setExt(jsonObject.toJSONString());
            omsTask.setRefCode(orderCode.toString());
            omsTask.setTaskType(TaskType.REFUND.getCode());
            omsTask.setVenderId(orderHead.getVenderId());
            omsTaskService.add(omsTask);
        }

    }

    /**
     * @param head
     * @return
     */
    private boolean validationOrderStatus(OrderHead head) {
        Long orderCode = head.getOrderCode();
        if (!head.getOrderStatus().equals(OrderStatus.ORDER_SIGN.getIndex()) && !head.getOrderStatus().equals(OrderStatus.ORDER_FINISH.getIndex())) {
            logger.info("【签收--check】 订单状态, 不是已送达和已完成!! orderCode: {}", orderCode);
            return false;
        }
        // 判断订单的支付类型,是否是在线支付
//        if (!PayType.PAY_ON_DELIVERY.getValue().equals(head.getPayType())) {
//            logger.info("【签收--check】 订单支付类型, 不是在线支付!! orderCode: {}", orderCode);
//            return false;
//        }
        // 判断订单的支付状态,是否是已支付
//        if (!PayStatus.PAID.getValue().equals(head.getPayStatus())) {
//            logger.info("【签收--check】 订单未支付!! orderCode: {}", orderCode);
//            return false;
//        }

        return true;
    }
}
